package com.chencs.biz.member.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.member.service.MemberService;
import com.chencs.vo.biz.member.MemberModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by fangzehua on 2017/7/4.
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getMemberList(){
        List<MemberModel> list = memberService.getMemberList();
        if(!list.isEmpty()){
            return JSON.toJSONString(list);
        }else{
            return ResponseOptUtil.commonMsgError("获取會員列表失败");
        }
    }


    @RequestMapping(value = "/find", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findMemberById(@RequestBody String id){
        MemberModel memberModel = memberService.findMemberById(id);
        if(memberModel!=null){
            return JSON.toJSONString(memberModel);
        }else{
            return ResponseOptUtil.commonMsgError("获取會員失败");
        }
    }

    @RequestMapping(value = "/findMemberBySid", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findMemberBySid(@RequestBody String sid){
        MemberModel memberModel = memberService.findMemberBySid(sid);
        if(memberModel!=null){
            return JSON.toJSONString(memberModel);
        }else{
            return ResponseOptUtil.commonMsgError("获取會員失败");
        }
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String insertMember(@RequestBody MemberModel memberModel){
        try {
            memberService.insertMember(memberModel);
            return ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return  ResponseOptUtil.commonMsgError("添加会员失败");
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateMember(@RequestBody MemberModel memberModel){
        try {
            memberService.updateMember(memberModel);
            return ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return  ResponseOptUtil.commonMsgError("添加会员失败");
        }
    }


}
